#########################
###     CHANGE ME!    ###

VERSE_PATH=../verse
TERMBOX_PATH=../termbox

###                   ###
#########################



########################################
### no need to change anything below ###
########################################
##for crux
LUA_LIB_PATH=/usr/lib
##for other, inferior ;) distros, that put lua in /usr/local
LUA_LIB_PATH2=/usr/local/lib

#gcc stuff
INCLUDE=/usr/include
CFLAGS=-O2 -march=native -s
LDFLAGS=-Wl,-O1 -Wl,--as-needed
BUILD_PATH=build
SRC=src
RUNTIME_NAME=gupozord
LIBS=-llua -ltermbox 
WARNINGS=-Wall -Wconversion

GCC=gcc ${CFLAGS} ${LDFLAGS} ${WARNINGS} -I${INCLUDE} -I${TERMBOX_PATH} -L. -L./${BUILD_PATH} -L${LUA_LIB_PATH} -L${LUA_LIB_PATH2} -L${TERMBOX_PATH} 

run: help

test: compile
	cd build && sh ./gupozord

dirs:
	mkdir -p build

all: clean-termbox clean compile-termbox compile-verse compile

compile-verse: dirs
	cd ${VERSE_PATH} && squish --use-http --no-minify
	cp ${VERSE_PATH}/verse.lua ${BUILD_PATH}/

clean-verse:
	cd ${VERSE_PATH} && rm -f ./verse.lua

compile-termbox: dirs
	cd ${TERMBOX_PATH} && make all
	cp ${TERMBOX_PATH}/libtermbox.so ${BUILD_PATH}/

clean-termbox:
	cd ${TERMBOX_PATH} && make clean

compile: dirs
	${GCC} ${SRC}/ltermbox.c -o ./${BUILD_PATH}/ltermbox.so -shared -fPIC ${LIBS}
	echo "env LD_LIBRARY_PATH=./:${LUA_LIB_PATH}:${LUA_LIB_PATH2}:${TERMBOX_PATH} lua ./gupozord.lua" > ./${BUILD_PATH}/${RUNTIME_NAME}
	chmod +x ./${BUILD_PATH}/${RUNTIME_NAME}
	cp -r ${SRC}/* ${BUILD_PATH}

clean:
	rm -fr ./${BUILD_PATH}

help:
	@echo ""
	@echo ""
	@echo ""
	@echo "     OH HAI!"
	@echo "     Thanks for downloading gupozord."
	@echo ""
	@echo ""
	@echo "     This help message can be invoked again, by typing 'make help' (without the quotes)."
	@echo ""
	@echo "     To build gupozord you'll need:"
	@echo "     	* verse library - get it here: http://code.matthewwild.co.uk/verse"
	@echo "     		* squish - required by verse - get it here: http://matthewwild.co.uk/projects/squish/home"
	@echo "     		* luasocket, luaexpat - verse needs those two too - don't have them? - get them using luarocks: http://luarocks.org/"
	@echo "     	* termbox - avaliable here: http://code.google.com/p/termbox/"
	@echo ""
	@echo "     INSTALLATION:"
	@echo "     1)  Install squish, luasocket and luaexpat."
	@echo "     2)  Unpack termbox and verse sources somewhere."
	@echo "     3a) run: 'VERSE=/path/to/verse TERMBOX=/path/to/termbox make all'"
	@echo "     3b) or just edit first two variables in Makefile and run 'make all'"
	@echo ""
	@echo "     Have fun, KTHXBAI!"
	@echo ""
	@echo ""

